Put the ipynb file and html file in the github branch you created in the last assignment and submit the link to the commit in brightspace
from plotly.offline import init_notebook_mode
import plotly.io as pio
import plotly.express as px
init_notebook_mode(connected=True)
pio.renderers.default = "plotly_mimetype+notebook"
#load data
df = px.data.gapminder()
df.head()
| country | continent | year | lifeExp | pop | gdpPercap | iso_alpha | iso_num | |
|---|---|---|---|---|---|---|---|---|
| 0 | Afghanistan | Asia | 1952 | 28.801 | 8425333 | 779.445314 | AFG | 4 |
| 1 | Afghanistan | Asia | 1957 | 30.332 | 9240934 | 820.853030 | AFG | 4 |
| 2 | Afghanistan | Asia | 1962 | 31.997 | 10267083 | 853.100710 | AFG | 4 |
| 3 | Afghanistan | Asia | 1967 | 34.020 | 11537966 | 836.197138 | AFG | 4 |
| 4 | Afghanistan | Asia | 1972 | 36.088 | 13079460 | 739.981106 | AFG | 4 |
Recreate the barplot below that shows the population of different continents for the year 2007.
Hints:
df_2007 = df[df["year"] == 2007]
df_2007 = df_2007.groupby("continent").sum()
print(df_2007["pop"].head())
print(df_2007.columns)
fig = px.bar(df_2007["pop"], color=df_2007.index, orientation="h", text_auto='.3s')
fig.show()
# YOUR CODE HERE
continent Africa 929539692 Americas 898871184 Asia 3811953827 Europe 586098529 Oceania 24549947 Name: pop, dtype: int64 Index(['year', 'lifeExp', 'pop', 'gdpPercap', 'iso_num'], dtype='object')
fig.update_layout(yaxis={'categoryorder':'total ascending'})
fig.show()
Add text to each bar that represents the population
fig.show()
Thus far we looked at data from one year (2007). Lets create an animation to see the population growth of the continents through the years
df_grouped = df.groupby(["continent","year"]).sum()
print(df_grouped.head(20))
df_grouped = df_grouped.reset_index(level=[0,1])
print(df_grouped.head(20))
fig = px.bar(df_grouped, y="continent", x="pop", animation_frame="year", range_x=[0,4000000000], orientation="h", color="continent")
fig.update_layout(yaxis={'categoryorder':'total ascending'})
fig.show()
# YOUR CODE HERE
lifeExp pop gdpPercap iso_num
continent year
Africa 1952 2035.046 237640501 65133.768223 23859
1957 2145.850 264837738 72032.275237 23859
1962 2252.611 296516865 83100.098892 23859
1967 2357.396 335289489 106618.917645 23859
1972 2467.449 379879541 121660.015058 23859
1977 2578.182 433061021 134468.802440 23859
1982 2682.829 499348587 129042.833907 23859
1987 2773.929 574834110 118698.787546 23859
1992 2788.738 659081517 118654.137329 23859
1997 2787.110 743832984 123695.496865 23859
2002 2772.912 833723916 135168.028262 23859
2007 2849.914 929539692 160629.695446 23859
Americas 1952 1331.996 345152446 101976.563805 9843
1957 1399.007 386953916 115401.093329 9843
1962 1459.969 433270254 122538.546760 9843
1967 1510.273 480746623 141706.337401 9843
1972 1559.873 529384210 162283.353476 9843
1977 1609.789 578067699 183800.178157 9843
1982 1655.721 630290920 187668.427202 9843
1987 1702.268 682753971 194835.006528 9843
continent year lifeExp pop gdpPercap iso_num
0 Africa 1952 2035.046 237640501 65133.768223 23859
1 Africa 1957 2145.850 264837738 72032.275237 23859
2 Africa 1962 2252.611 296516865 83100.098892 23859
3 Africa 1967 2357.396 335289489 106618.917645 23859
4 Africa 1972 2467.449 379879541 121660.015058 23859
5 Africa 1977 2578.182 433061021 134468.802440 23859
6 Africa 1982 2682.829 499348587 129042.833907 23859
7 Africa 1987 2773.929 574834110 118698.787546 23859
8 Africa 1992 2788.738 659081517 118654.137329 23859
9 Africa 1997 2787.110 743832984 123695.496865 23859
10 Africa 2002 2772.912 833723916 135168.028262 23859
11 Africa 2007 2849.914 929539692 160629.695446 23859
12 Americas 1952 1331.996 345152446 101976.563805 9843
13 Americas 1957 1399.007 386953916 115401.093329 9843
14 Americas 1962 1459.969 433270254 122538.546760 9843
15 Americas 1967 1510.273 480746623 141706.337401 9843
16 Americas 1972 1559.873 529384210 162283.353476 9843
17 Americas 1977 1609.789 578067699 183800.178157 9843
18 Americas 1982 1655.721 630290920 187668.427202 9843
19 Americas 1987 1702.268 682753971 194835.006528 9843
Instead of the continents, lets look at individual countries. Create an animation that shows the population growth of the countries through the years
# YOUR CODE HERE
df = px.data.gapminder()
fig = px.bar(df, y="country", x="pop", animation_frame="year", animation_group="country", color="continent", hover_name="country",
range_x=[0,1500000000])
fig.update_layout(barmode='stack', yaxis={'categoryorder':'total descending'})
fig.show()
Clean up the country animation. Set the height size of the figure to 1000 to have a better view of the animation
# YOUR CODE HERE
fig.update_layout(height=1000)
fig.show()
# YOUR CODE HERE
fig.update_layout(yaxis_range=[-0.5,9.5], height=400)
fig.show()